import React,{useEffect} from 'react'
import { useHistory,useLocation } from 'react-router-dom/cjs/react-router-dom.min'

function yztoken(Com) { //1.接收一个组件为参数
    //2.返回一个新组件
    return function token(props) {
        let history = useHistory()
        let location = useLocation()
        //如果不是登录页
        if(location.pathname!=='/login'){
            //验证token是否存在
            let token = localStorage.getItem('token')
            if(!token){
                //跳转到登录页登录
                history.push('/login')
            }
        }
        return (
            <>
                <Com {...props}/>
            </>
        )
    }
}

export default yztoken
